<?php

namespace app\web\model;

use app\common\model\PlanPayment;
use app\common\model\UserCreditCard;
use app\common\model\Users;
use app\common\model\CreditBanks;
use think\Db;
use think\Model;

class LanchainApi extends Model
{

    public function requestOpen($user_card)
    {
        $bank_info = CreditBank::get($user_card->bank_id);
        $user = Users::get($user_card->uid);
        $data      = [
            'mercName'        => $user_card->name_on_card,
            'idNo'            => $user_card->id_number,
            'bankLeaveMobile' => $user_card->credit_mobile,
            'mobile'          => $user->mobile,
            'accountNo'       => $user_card->getData('full_card_number'),
            'accountName'     => $user_card->name_on_card,
            'openBankName'    => $bank_info->bank_name,
            'bankCode'        => $bank_info->bank_code_en,
            'issuerCode'      => str_pad($bank_info->bank_code, 12, 0, STR_PAD_RIGHT),
            'issuerName'      => $bank_info->bank_name,
            'cityId'          => $bank_info->city_code,
            'cityName'        => $bank_info->city_name,
            'provinceId'      => $bank_info->province_code,
            'provinceName'    => $bank_info->province_name
        ];
        return $data;
    }

    public function requestDeduct($deduct)
    {
        $user_info = Users::get($deduct['uid']);
        $credit_info = UserCreditWeb::get($deduct['user_credit_id']);
        list($m,$y) = explode('/', $credit_info['credit_valid_date']);
        $data = [
            'mercOrderId'=> $deduct['deduct_mercOrderId'],
            'mercId'=> $user_info->mercId,
            'transAmt'=> $deduct['deduct_money'] * 100,
            'accountNo'=> $credit_info['full_card_number'],
            'bankLeaveMobile'=> $credit_info['credit_mobile'],
            'openBankName'=> $credit_info->getBankInfo->bank_name,
            'bankCode'=> $credit_info->getBankInfo->bank_code_en,
            'cvn2'=> $credit_info['credit_verification_code'],
            'expireDate'=> $m.$y
        ];
        return $data;
    }

    public function requestPay($cardId, $payment_id)
    {
        $credit_info = UserCreditCard::get($cardId);
        list($m,$y) = explode('/', $credit_info->credit_valid_date);
        $payment = PlanPayment::get($payment_id);
        $data = [
            'mercOrderId' => $payment->payment_mercOrderId,
            'mercQuickOrderIds'	 => $payment->planDatail->deduct_mercOrderId,
            'mercId'=>$credit_info->userInfo->mercId,
            'transAmt'	 => $payment->payment_money * 100,
            'accountNo'	 => $credit_info->full_card_number,
            'bankLeaveMobile'	 => $credit_info->credit_mobile,
            'openBankName'	 => $credit_info->bank_name,
            'bankCode'	 => $credit_info->bankInfo->bank_code_en,
            'cvn2'	 => $credit_info->credit_verification_code,
            'expireDate'	 => $m.$y
        ];
        return $data;
    }

    public function requestSms($creditId)
    {
        $credit_info = UserCreditCard::get($creditId);
        list($m, $y) = explode('/', $credit_info['credit_valid_date']);
        $user = Users::get(['uid' => $credit_info['uid']]);
        $data = [
            'mercId'          => $user->mercId,
            'accountNo'       => $credit_info['full_card_number'],
            'accountName'     => $credit_info['name_on_card'],
            'crpIdNo'         => $credit_info->id_number,
            'bankCode'        => $credit_info->bankInfo['bank_code_en'],
            'bankName'        => $credit_info->bankInfo['bank_name'],
            'cvn2'            => $credit_info['credit_verification_code'],
            'expireDate'      => $m . $y,
            'bankLeaveMobile' => $credit_info['credit_mobile'],
            'channelId'       => $credit_info['channel_id']
        ];
        return $data;
    }


}
